package net.huashitong.supervision.task.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.HastenMessage;
import net.huashitong.supervision.task.filter.HastenMessageFilter;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/29
 * Time: 20:52
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Repository
public class HastenMessageDao extends StringDao<HastenMessage> {
    public List<HastenMessage> queryFilter(HastenMessageFilter filter) {
        GenericQuery query = createQuery("obj");
        if (StringUtils.isNotBlank(filter.getContent())){
            query.append(" and content like:content").setParam("content", StringUtils.getLikewords(filter.getContent()));
        }
        if (StringUtils.isNotBlank(filter.getCreateId())){
            query.append(" and createUser =:createUser").setParam("createUser",filter.getCreateId());
        }
        if (StringUtils.isNotBlank(filter.getUserId())){
            query.append(" and userId =:userId").setParam("userId",filter.getUserId() );
        }
        if(StringUtils.isNotBlank(filter.getDeptId())){
            query.append(" and deptId =:deptId").setParam("deptId",filter.getDeptId() );
        }
        if(StringUtils.isNotBlank(filter.getTaskUnitId())){
            query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",filter.getTaskUnitId() );
        }
        //设置排序
        if (ObjectUtils.isNotBlank(filter.getPaging())) {
            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
                filter.getPaging().setSortCol("sysId");
                filter.getPaging().setSortVal("desc");
            }
        }else{
            Paging paging = new Paging(0);
            paging.setSortCol("sysId");
            paging.setSortVal("desc");
            filter.setPaging(paging);
        }
        return query.listResult(filter);
    }

    public void deleteByUnitIds(List<String> taskUnitIds) {
        StringBuffer hql = (new StringBuffer("delete from HastenMessage")).append(" obj where obj.taskUnitId in(:taskUnitIds)");
        Query query = this.getSession().createQuery(hql.toString());
        query.setParameterList("taskUnitIds", taskUnitIds);
        query.executeUpdate();
    }
}
